package com.yazi.mxz.person.controller.firm;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.service.GoodsClassifyService;

@Controller("firm-goodsClassifyController")
@RequestMapping("/firm/goodsClassify")
public class GoodsClassifyController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsClassifyController.class);

	@Resource
	GoodsClassifyService goodsClassifyService;

	@RequestMapping("/list")
	public String list(Model model) {
		debug(log, "进入企业商品类目列表页面");
		model.addAttribute("companyId", getCompanyId());
		return "firm/goodsClassify_list";
	}

	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public ObjectResult<?> addOrEditDoJson(GoodsClassify classify) {
		debug(log, "添加/编辑企业商品类目");
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		try {
			if (null == classify.getClassifyId()) {
				classify.setSort(DateUtil.getTimeStamp());// 新添加的类排在前面
			}
			classify = goodsClassifyService.addOrEditByCompany(getCompany(true), classify);
			jsonResult.setResult(classify.getClassifyId());
			jsonResult.setOk();
		} catch (MyException e) {
			debug(log, "添加/编辑企业商品类目失败：" + e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "添加/编辑企业商品类目异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/sortDo/json")
	@ResponseBody
	public StatusResult sortDoJson(long classifyId) {
		debug(log, "企业商品类目排序");
		StatusResult jsonResult = new StatusResult();
		GoodsClassify classify;
		try {
			classify = goodsClassifyService.one(classifyId);
			if (classify.getCompany().equals(getCompany(true))) {
				classify.setSort(DateUtil.getTimeStamp());
				goodsClassifyService.merge(classify);
			}
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "企业商品类目排序异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long delId, long bakupId) {
		debug(log, "删除企业商品类目");
		StatusResult jsonResult = new StatusResult();
		try {
			goodsClassifyService.removeByCompany(getCompany(true), delId, bakupId);
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "企业商品类目删除异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
}
